package com.yixun.lbx.controller.farm;

import com.yixun.controller.BaseController;
import com.yixun.lbx.entity.YxProBeeBreed;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.service.BeeBreedServie;
import com.yixun.lbx.service.CompanyService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/bb")
public class BeeBreedController extends BaseController {
    private final BeeBreedServie beeBreedServie;
    private final CompanyService companyService;

    public BeeBreedController(BeeBreedServie beeBreedServie, CompanyService companyService) {
        this.beeBreedServie = beeBreedServie;
        this.companyService = companyService;
    }

    /**
     *获取繁育列表
     */
    @PostMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        Integer count = beeBreedServie.getCount(map);
        List<YxProBeeBreed> beeBreeds = beeBreedServie.getListByPage(map);

        map.put("count", count);
        map.put("beeBreeds", beeBreeds);
        return map;
    }

    /**
     *添加繁育
     */
    @PostMapping("/addBeeBreed")
    public int addBeeHive(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        map.put("addUser", this.getSessionUser().getUser_id());
        return beeBreedServie.addBeeBreed(map);
    }

    /**
     *修改繁育
     */
    @PostMapping("/updateBeeBreed")
    public int updateBeeHive(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] ids = map.get("id").toString().split(",");
        map.put("ids", ids);
        return beeBreedServie.updateBeeBreed(map);
    }

    /**
     *删除繁育
     */
    @PostMapping("/deleteBeeBreed")
    public int deleteBeeHive(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] ids = map.get("id").toString().split(",");
        map.put("ids", ids);
        return beeBreedServie.deleteBeeBreed(map);
    }

    @GetMapping("/getObjByArgs")
    public List<YxProBeeBreed> getObjByArgs(YxProBeeBreed beeBreed) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            beeBreed.setQyId(String.valueOf(qyId));
        }
        return beeBreedServie.getObjByArgs(beeBreed);
    }

    /**
     * 搜索
     * */
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        map.put("qyId", String.valueOf(this.getQyId()));
        List<YxProBeeBreed> obj = beeBreedServie.getSearchListByPage(map);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("obj", obj);
        hashMap.put("count", obj.size());
        return hashMap;
    }

    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }
}
